﻿using Google.Protobuf.WellKnownTypes;
using HandyControl.Controls;
using HandyControl.Data;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Common;
using ZoneLineNew.Models.Dto;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;
using ZoneLineNew.Views;

namespace ZoneLineNew {

    public class MainWindowModel : NavigationViewModel {
        public MainWindowModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IMessageService messageBox,
            IEventAggregator eventAggregator, 
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {


        }

        private string _title = AppSession.AppactionName;

        public string Title {
            get { return _title; }
            set { _title = value; RaisePropertyChanged(); }
        }

        private int _SelectedIndex;
        public int SelectedIndex {
            get { return _SelectedIndex; }
            set { _SelectedIndex = value; RaisePropertyChanged(); }
        }

        private MenuItemDto _SelectedItem;
        public MenuItemDto SelectedItem {
            get { return _SelectedItem; }
            set {
                if (value == null || string.IsNullOrWhiteSpace(value.Code))
                    return;
            }
        }

        public DelegateCommand<FunctionEventArgs<object>> SwitchItemCmd => new DelegateCommand<FunctionEventArgs<object>>(obj => {
            if (obj.Info is SideMenuItem item) {
                _regionManager.Regions[PrismRegionManager.MainViewRegionName].RequestNavigate(item.Name, arg => {
                    //没有导航成功 
                    if (arg.Result == false) {
                        if (arg.Error != null) {
                            _message.GrowError($"导航失败，pageParams：{item.Name}，errorInfo：{arg.Error.Message}");
                        }
                    } else {
                        Console.WriteLine("导航成功");
                    } 
                });
            }
        });

        protected override void OnViewLoaded() {
            var arg = new FunctionEventArgs<object>(null, null);
            arg = new FunctionEventArgs<object>(null, null) { Info = new SideMenuItem() { Name = nameof(MainView) } };
            if (Cache.IsHopStaion) {
                arg = new FunctionEventArgs<object>(null, null) { Info = new SideMenuItem() { Name = nameof(MainTwoView) } };
            }
            SwitchItemCmd.Execute(arg);
            base.OnViewLoaded();
        }

    }
}
